package com.gzmu.myprint.mapper;


import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.gzmu.myprint.model.*;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface RepairMapper extends BaseMapper<PayMent> {
//    @Select("SELECT branch FROM bx_branch WHERE branch LIKE CONCAT(#{keyword},'%')")
//    List<Branch> selectAllBranch(String keyword);

    @Select("SELECT branch FROM bx_branch")
    List<String> branchList();

//    @Insert("INSERT INTO bx_user(wxid,openid,nick,name,gender,img,phone,branch,title,role) " +
//            "VALUES (#{wxid},#{openid},#{nick},#{name},#{gender},#{img},#{phone},#{branch},#{title},#{role})" +
//            "ON DUPLICATE KEY UPDATE wxid = #{wxid}, openid = #{openid}, nick = #{nick}, name = #{name}," +
//            "gender = #{gender}, img = #{img}, phone = #{phone}, branch = #{branch}, title = #{title}, role = #{role}")
//    @Options(useGeneratedKeys = true, keyProperty = "openid", keyColumn = "openid")
//    int insertTeacher(Teacher teacher);
    @Insert("INSERT INTO bx_user(openid) VALUES (#{openid})")
    @Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")
    int insertTeacher(Teacher teacher);

    @Update("UPDATE bx_user SET wxid=#{wxid}, nickName=#{nickName}, name=#{name}, gender=#{gender}, avatarUrl=#{avatarUrl}, "+
            "phone=#{phone}, branch=#{branch}, title=#{title}, role=#{role} WHERE openid=#{openid}")
    @Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")
    int updateTeacher(Teacher teacher);

    @Select("SELECT * FROM bx_user WHERE openid=#{openid}")
    Teacher findUerByOpenId(@Param("openid") String openid);

    @Select("SELECT max(id) FROM bx_user")
    int getLastUid();

    @Select("SELECT distinct build FROM bx_build")
    List<String> selectAllBuild();

    @Select("SELECT distinct floor FROM bx_build WHERE build=#{build}")
    List<String> selectAllFloor(String build);

    @Select("SELECT distinct room FROM bx_build WHERE build=#{build} AND floor=#{floor}")
    List<String> selectAllRoom(Build build);


    @Insert("INSERT INTO bx_file(openid,oldName,urlName,time,isEnble) " +
            "VALUES (#{openid},#{oldName},#{urlName},#{time},#{isEnble})")
    @Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")
    int insertImgFile(ImgFile file);

    @Insert("INSERT INTO bx_repair(openid,build,floor,room,facility,tid,tname,timg,tfanlt,ttime,wid,wname,wimg,wtime,wfanlt,state) " +
            "VALUES (#{openid},#{build},#{floor},#{room},#{facility},#{tid},#{tname},#{timg},#{tfanlt},#{ttime},#{wid}" +
            ",#{wname},#{wimg},#{wtime},#{wfanlt},#{state})")
    @Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")
    int insertRepair(Repair repair);

    @Update("UPDATE bx_repair SET wid=#{wid},wname=#{wname},wimg=#{wimg},wtime=#{wtime},wfanlt=#{wfanlt},state=#{state} WHERE id=#{id}")
    @Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")
    int updateRepair(Repair repair);

    @Select("SELECT * FROM bx_repair WHERE openid=#{openid} AND state=#{state}")
    List<Repair> selectRepair(Repair repair);

    @Select("SELECT * FROM bx_repair WHERE id=#{id}")
    Repair selectRepairById(String id);

    @Select("SELECT * FROM bx_repair WHERE state=#{state}")
    List<Repair> selectRepairByState(String state);

    @Select("SELECT * FROM bx_repair WHERE  wid=#{wid} AND state=#{state}")
    List<Repair> selectRepairByWidState(Repair repair);

    @Select("select sum(case when state=0 then 1 else 0 end) as 'wait',sum(case when state=1 "+
            "then 1 else 0 end) as 'finish' from bx_repair where openid=#{openid}")
    Status totalRepairState(String openid);
}
